var configData = false;
var jspxFrom = false;
var loginDialog = false;
var showFormIndex = true;

//淘汰的登录对话框
var oldloginDlg=function(still,btn)
{
    if (still==undefined) still = 0;
    var myIFrame = new IFrame({allowTransparency:'true',frameborder:0,"src":"/user/loginmodule.${suffix}?still=" + still,
        styles: {
            width:'99%',
            height:400,
            overflow:'hidden'
        }
    });

    loginDialog = new JDialog.Alert({
        title: '登录窗口',
        winIconCss:'winUserIcon',
        resize:false,
        message: myIFrame,
        onBeforeClose:function(v)
        {
            if (v==0) return;
            var aft = typeOf(window.document.loginAfter);
            if (aft&& aft!='null') window.document.loginAfter();
            if (btn)
            {
                btn.set('type','submit');
                btn.set('value','提 交');
                btn.onclick = null;
            }
            else  window.location.reload();
        }
    });
    loginDialog.show();
};
//新的登录对话框（考虑是否替换,更换登录意味着所有可以登录的页面都要加载jbCommon以及jq一共三个文件）
function loginDlg() {
    if (loginDialog){
        loginDialog.show();
    } else {
        new Request.HTML({
            'async': false,
            'url':'${remoteHostUrl}/user/loginmodule.'+suffix,
            onSuccess:function (result) {
                loginDialog = new Jb_popup({
                    con:jQuery(result),
                    class:"loginDialog",
                    titCon:"登录",
                    yesCon:false,
                    noCon:false,
                }).show();
            }
        }).send();
    }
}

function registerDlg()
{
    var myIFrame = new IFrame({allowTransparency:'true',frameborder:0,"src":"/user/reguser.${suffix}",
        styles: {
            width:'99%',
            height:560,
            overflow:'hidden'
        }
    });

    var dialog = new JDialog.Alert({
        title: '注册窗口',
        width:720,
        winIconCss:'winUserIcon',
        resize:true,
        message: myIFrame
    });
    dialog.show();
}

function editUser()
{
    var myIFrame = new IFrame({allowTransparency:'true',frameborder:0,"src":"/user/viuser.${suffix}",
        styles: {
            width:'99%',
            height:720,
            overflow:'hidden'
        }
    });

    var dialog = new JDialog.Alert({
        title: '编辑窗口',
        width:720,
        winIconCss:'winUserIcon',
        resize:true,
        message: myIFrame
    });
    dialog.show();
    dialog.maxClick();
}

//添加关注
function memeberFollow(el)
{
    var uid = el.get('ref');
    var posts = {
        "version": "3.0",
        "method":
            {
                "name": "follow",
                "params": {
                    "uid": uid
                }
            }
    };

    new Request.ROC({'url':'/user/userfollow.' + suffix,'data':posts,
        onSuccess:function(obj)
        {
            if (obj.success==1)
            {
                el.set('html','已关注');
            } else {
                var dlg = new JDialog.Alert({
                    title: '消息提示',
                    message:obj.message
                });
                dlg.show();
            }
        }
    }).send();
}

function cancelFollow(el)
{
    var uid = el.get('ref');
    var posts = {
        "version": "3.0",
        "method":
            {
                "name": "cancelFollow",
                "params": {
                    "uid": uid
                }
            }
    };

    new Request.ROC({'url':'/user/userfollow.' + suffix,'data':posts,
        onSuccess:function(obj)
        {
            if (obj.success==1)
            {
                el.set('html','关注');
            } else {
                var dlg = new JDialog.Alert({
                    title: '消息提示',
                    message:obj.message
                });
                dlg.show();
            }
        }
    }).send();
}

function addContacts(uid)
{
    var event=window.document.getEvent();
    var point = window.document.mousePos(event);
    var myIFrame = new IFrame({allowTransparency:'true',frameborder:0,"src":"/user/aecontacts.${suffix}?uid="+uid,
        styles: {
            width:'100%',
            overflow:'hidden',
            height:'360px'
        }
    });

    var dialog = new JDialog.Alert({
        title: '添加联系人',
        width:'360px',
        winIconCss:'winUserIcon',
        resize:true,
        message: myIFrame
    });
    dialog.show();
}

function followToContacts(ids)
{
    var posts = {
        "version": "3.0",
        "method":
            {
                "name": "followSave",
                "params": {
                    "ids": ids
                }
            }
    };

    new Request.ROC({'url':'/user/aecontacts.' + suffix,'data':posts,
        onSuccess:function(obj)
        {

            var dlg = new JDialog.Alert({
                title: '消息提示',
                message:obj.messages
            });
            dlg.show();
        }
    }).send();
}

function findPasswordDlg()
{
    var event=window.document.getEvent();
    var point = window.document.mousePos(event);
    var myIFrame = new IFrame({allowTransparency:'true',frameborder:0,"src":"/user/findpassword.${suffix}",
        styles: {
            width:'580px',
            overflow:'hidden',
            height:'280px'
        }
    });

    var dialog = new JDialog.Alert({
        title: '找回密码窗口',
        width:600,
        top:point.y,
        left:window.screen.availWidth/2-300,
        winIconCss:'winUserIcon',
        resize:true,
        message: myIFrame
    });
    dialog.show();
}

function sendMessage(id,name)
{

    window.open('/user/sendmsgdlg.${suffix}?uid='+id, name)
    /*

     var myIFrame = new IFrame({allowTransparency:'true',frameborder:0,
                                       "src":'/user/sendmsgdlg.${suffix}?uid='+id,
                                       styles: {
                                            width:'100%',
                                            height:'500px'
                                        }
                                    });

     var dialog = new JDialog.Alert({
                                title: '消息对话框',
                                width:760,
                                resize:true,
                                message: myIFrame
                            });
     dialog.show();
     */
}


function showMessageDlg()
{
    var myIFrame = new IFrame({allowTransparency:'true',frameborder:0,
        "src":'/user/listmsgdlg.${suffix}?boxType=5',
        styles: {
            width:'100%',
            height:'500px'
        }
    });

    var dialog = new JDialog.Alert({
        title: '消息对话框',
        width:760,
        resize:true,
        message: myIFrame
    });
    dialog.show();
}

function showSendMessageDlg(uid)
{
    var myIFrame = new IFrame({allowTransparency:'true',frameborder:0,
        "src":'/user/sendmsgdlg.${suffix}?boxType=1&uid='+ uid,
        styles: {
            width:'100%',
            height:'500px'
        }
    });

    var dialog = new JDialog.Alert({
        title: '消息对话框',
        width:760,
        resize:true,
        message: myIFrame
    });
    dialog.show();
}

function showWebcomplainDialog(cont)
{
    var myIFrame = new IFrame({allowTransparency:'true',frameborder:0,
        "src":"/user/addwebcomplain.${suffix}?url="+window.location+'&cont='+cont,
        styles:
            {
                width:'99%',
                height:400,
                overflow:'hidden'
            }});

    var dialog = new JDialog.Alert({
        title: '非法举报',
        width:680,
        message: myIFrame
    });
    dialog.show();
}

function addFavorite(linkPage,title,nodeId)
{
    var myIFrame = new IFrame({allowTransparency:'true',frameborder:0,
        "src":'/user/aefavorite.${suffix}?linkPage=' + encodeURI(linkPage) + '&title='+ title  + '&nodeId='+ nodeId ,
        styles: {
            width:'100%',
            height:'400px'
        }
    });

    var dialog = new JDialog.Alert({
        title: ' 添加收藏',
        width:810,
        resize:true,
        message: myIFrame
    });
    dialog.show();
}
function showFavorite()
{
    var myIFrame = new IFrame({allowTransparency:'true',frameborder:0,
        "src":'/user/listfavorite.${suffix}?boxType=1',
        styles: {
            width:'100%',
            height:'460px'
        }
    });
    var dialog = new JDialog.Alert({
        title: ' 我的收藏',
        width:740,
        resize:true,
        message: myIFrame
    });
    dialog.show();
}

function showUserInfo(id)
{
    var myIFrame = new IFrame({allowTransparency:'true',frameborder:0,
        "src":'/user/userinfo.${suffix}?id=' + id,
        styles: {
            width:'100%',
            height:'470px'
        }
    });
    var dialog = new JDialog.Alert({
        title: '用户信息',
        width:720,
        resize:true,
        message: myIFrame
    });
    dialog.show();

}

var imgDialog = false;
function showImageDialog(link,info) {
    if (imgDialog) imgDialog.close();
    var imgEl = new Element('img',{'src':link,'title':info,'styles':{'margin':'5px','padding-top':'5px',border:0,'max-height':620,'max-width':620}});
    var el = new Element('a',{'href':link,'target':'_new'});
    el.adopt(imgEl);
    imgDialog = new JDialog.Alert({title: info,message:el});
    imgDialog.show();
}

var linkDialog = false;
function showLinkDialog(link,info) {
    if (linkDialog) linkDialog.close();
    if (link=='') return;
    if (link.startsWith('[')&&link.endWith(']'))
    {
        link = link.substring(1,link.length-1);
    }

    var dlgIFrame = new IFrame({allowTransparency:'true',frameborder:0,src:link,
        styles: {
            width:'100%',
            height:'600px'
        }
    });
    linkDialog = new JDialog.Alert({
        title: info,
        width:700,
        height:600,
        message:dlgIFrame
    });
    linkDialog.show();
}

function insertFile(hvar,title,id) {

    var url = getBrowserURL();
    if (!hvar.startsWith('http')&&!hvar.startsWith('/')) {
        if (url.countMatches('/') >= 4) {   //得到上一级目录
            var first = url.lastIndexOf('/') != -1;
            if (first) {
                first = url.lastIndexOf('/') - 1;
                url = url.substring(0, first);
            }
            if (url.lastIndexOf('/') != -1) {
                first = url.lastIndexOf('/') + 1;
                url = url.substring(0, first);
            }
        }
        hvar = url + hvar;
    }
    var fileExt = hvar.substringLastAfter('.').toLowerCase();
    var htmlCode = "";
    if (fileExt == "wmv") {
        htmlCode = "<object id=\"mplayer\" width=\"460\" height=\"68\" classid=\"CLSID:22d6f312-b0f6-11d0-94ab-0080c74c7e95\" codebase=\"http://activex.microsoft.com/activex/controls/mplayer/en/nsmp2inf.cab#Version=6,4,5,715\" align=\"baseline\" border=\"0\" standby=\"Loading Microsoft Windows Media Player components...\" type=\"application/x-oleobject\">\n" +
            "            <param name=\"FileName\" value=\"" + hvar + "\">\n" +
            "            <param name=\"ShowControls\" value=\"1\">\n" +
            "            <param name=\"ShowAudioControls\" value=\"1\">\n" +
            "            <param name=\"ShowPositionControls\" value=\"0\">\n" +
            "            <param name=\"ShowTracker\" value=\"1\">\n" +
            "            <param name=\"ShowStatusBar\" value=\"1\">\n" +
            "            <param name=\"AutoStart\" value=\"1\">\n" +
            "            <param name=\"EnableContextMenu\" value=\"1\">\n" +
            "            <param name=\"InvokeURLs\" value=\"1\">\n" +
            "            <param name=\"DefaultFrame\" value=\"datawindow\">\n" +
            "</object>";
    } else if (fileExt == "swf") {
        htmlCode = "<embed quality=\"high\" type=\"application/x-shockwave-flash\" src=\"" + hvar + "\" style=\"WIDTH:100%; HEIGHT: 480px\">";
    } else if (fileExt == "rm" || fileExt == "rmvb") {
        htmlCode = "<OBJECT ID=video CLASSID=\"clsid:CFCDAA03-8BE4-11cf-B84B-0020AFBBCCFA\" HEIGHT=288 WIDTH=352>\n" +
            "<param name=\"_ExtentX\" value=\"9313\">\n" +
            "<param name=\"_ExtentY\" value=\"7620\">\n" +
            "<param name=\"AUTOSTART\" value=\"1\">\n" +
            "<param name=\"SHUFFLE\" value=\"1\">\n" +
            "<param name=\"PREFETCH\" value=\"0\">\n" +
            "<param name=\"NOLABELS\" value=\"0\">\n" +
            "<param name=\"SRC\" value=\"" + hvar + "\">\n" +
            "<param name=\"CONTROLS\" value=\"ImageWindow\">\n" +
            "<param name=\"CONSOLE\" value=\"Clip1\">\n" +
            "<param name=\"LOOP\" value=\"0\">\n" +
            "<param name=\"NUMLOOP\" value=\"0\">\n" +
            "<param name=\"CENTER\" value=\"0\">\n" +
            "<param name=\"MAINTAINASPECT\" value=\"0\">\n" +
            "<param name=\"BACKGROUNDCOLOR\" value=\"#000000\">\n" +
            "</OBJECT>";
    } else if (fileExt == "qt")
        htmlCode = "<object classid=clsid:02BF25D5-8C17-4B23-BC80-D3488ABDDC6B id=QTActiveXPlugin1 width=320 height=152><param name=_ExtentX value=0><param name=_ExtentY value=0><PARAM name=SRC VALUE=\"" + hvar + "\"></object>";
    else if (fileExt == "mpg") {
        htmlCode = "<object classid=\"clsid:05589FA1-C356-11CE-BF01-00AA0055595A\" id=\"ActiveMovie1\" width=\"400\" height=\"350\">\n" +
            "<param name=\"AutoStart\" value=\"-1\">\n" +
            "<param name=\"AllowChangeDisplayMode\" value=\"-1\">\n" +
            "<param name=\"AllowHideControls\" value=\"-1\">\n" +
            "<param name=\"AutoRewind\" value=\"-1\">\n" +
            "<param name=\"Enabled\" value=\"-1\">\n" +
            "<param name=\"EnableContextMenu\" value=\"-1\">\n" +
            "<param name=\"EnablePositionControls\" value=\"-1\">\n" +
            "<param name=\"EnableSelectionControls\" value=\"0\">\n" +
            "<param name=\"EnableTracker\" value=\"-1\">\n" +
            "<param name=\"Filename\" value=\"" + hvar + "\" valuetype=\"ref\">\n" +
            "<param name=\"PlayCount\" value=\"1\">\n" +
            "<param name=\"Rate\" value=\"1\">\n" +
            "<param name=\"SelectionStart\" value=\"-1\">\n" +
            "<param name=\"SelectionEnd\" value=\"-1\">\n" +
            "<param name=\"ShowControls\" value=\"-1\">\n" +
            "<param name=\"ShowDisplay\" value=\"-1\">\n" +
            "<param name=\"ShowTracker\" value=\"-1\">\n" +
            "</object>";
    } else if (fileExt == "wav") {
        htmlCode = "<object id=\"NSPlay\" width=300 height=300 classid=\"CLSID:22d6f312-b0f6-11d0-94ab-0080c74c7e95\" codebase=\"http://activex.microsoft.com/activex/controls/mplayer/en/nsmp2inf.cab#Version=6,4,5,715\" standby=\"Loading Microsoft Windows Media Player components...\" type=\"application/x-oleobject\" hspace=\"5\">\n" +
            "<param name=\"AutoRewind\" value=1>\n" +
            "<param name=\"FileName\" value=\"" + hvar + "\">\n" +
            "<param name=\"ShowControls\" value=\"1\">\n" +
            "<param name=\"ShowPositionControls\" value=\"0\">\n" +
            "<param name=\"ShowAudioControls\" value=\"1\">\n" +
            "</object>";
    } else if (fileExt == "jpg" || fileExt == "gif" || fileExt == "png" || fileExt == "bmp") {
        htmlCode = "<img src=\"" + hvar + "\" border=\"0\"/>";
    } else if (fileExt == "flv") {
        htmlCode = "<embed src=\"/script/vcastr2.swf\" FlashVars=\"vcastr_file=" + hvar + "\" quality=\"high\" pluginspage=\"http://www.macromedia.com/go/getflashplayer\" type=\"application/x-shockwave-flash\" width=\"550\" height=\"400\"></embed>";
    }
    else {
        htmlCode = "<a href='" + hvar + "' class='pvFile' name='" + fileExt + '_'+ id + "' ><img src='/share/pimg/filetype/" + fileExt + ".gif'>" + title + "</a>";
    }
    if (editor.insertHtml)
        editor.insertHtml(htmlCode);
    else
        editor.execCommand("insertHtml",htmlCode);

}

function builderTag(box) {
    var cont = '';
    if (typeof(editor)!='undefined'&&editor.getContentTxt) cont =editor.getContentTxt();
    if (typeof(editor)!='undefined'&&editor.getValue) cont =editor.getValue();
    else cont = editor.getContent();

    cont = cont.deleteHtml();
    if ($('title')) cont = cont + $('title').get('value');
    cont = cont.cut(1000);
    new Request({
        method:'post',
        url:'/descriptionPortal.${suffix}',
        onSuccess:function (msg) {
            if (tagBox)
            {
                var tagArray=msg.split(' ');
                for (var i=0; i<tagArray.length;i++)
                {
                    tagBox.add(tagArray[i]);
                }
            } else $(box).set('value', msg);
        }
    }).send('type=tags&content='+encodeURI(cont));
};

function builderDesc(box) {
    var cont = '';
    if (typeof(editor)!='undefined'&&editor.getContentTxt) cont =editor.getContentTxt();
    if (typeof(editor)!='undefined'&&editor.getValue) cont =editor.getValue();
    else cont = editor.getContent();

    cont = cont.deleteHtml();
    if (cont.length > 1000) cont = cont.cut(1000);
    new Request({
        method:'post',
        url:'/descriptionPortal.${suffix}',
        onSuccess:function (msg) {
            $(box).set('value', msg);
        }
    }).send('type=desc&content=' + encodeURI(cont));
}


/*
function scrollToTop(acceleration, time) {
    acceleration = acceleration || 0.1;
    time = time || 16;
    var x1 = 0;
    var y1 = 0;
    var x2 = 0;
    var y2 = 0;
    var x3 = 0;
    var y3 = 0;

    if (document.documentElement) {
        x1 = document.documentElement.scrollLeft || 0;
        y1 = document.documentElement.scrollTop || 0;
    }
    if (document.body) {
        x2 = document.body.scrollLeft || 0;
        y2 = document.body.scrollTop || 0;
    }

    var x3 = window.scrollX || 0;
    var y3 = window.scrollY || 0;

    // 滚动条到页面顶部的水平距离
    var x = Math.max(x1, Math.max(x2, x3));
    // 滚动条到页面顶部的垂直距离
    var y = Math.max(y1, Math.max(y2, y3));

    // 滚动距离 = 目前距离 / 速度, 因为距离原来越小, 速度是大于 1 的数, 所以滚动距离会越来越小
    var speed = 1 + acceleration;
    window.scrollTo(Math.floor(x / speed), Math.floor(y / speed));

    // 如果距离不为零, 继续调用迭代本函数
    if(x > 0 || y > 0) {
        var invokeFunction = "scrollToTop(" + acceleration + ", " + time + ")";
        window.setTimeout(invokeFunction, time);
    }
}
*/

function bindGotoTop(gotoTop)
{
    if (typeOf(gotoTop)!='element')
    {
        gotoTop = $(gotoTop)
    }
    var gotoFx = new Fx.Morph(gotoTop);
    gotoTop.addEvent('click',function(e) {
        scrollToTop();
    });
    gotoFx.start({'height':0,'opacity':0});
    window.addEvent('scroll',function(e) {
        if(Browser.ie6)
        {
            gotoTop.setStyles({
                'position': 'absolute',
                'bottom': window.getPosition().y + 10
            });
        }
        if (window.getScroll().y > 300)
        {
            gotoFx.start({'opacity':1,'height':36});
        }
        else {
            gotoFx.start({'height':0,'opacity':0});
        }
    });
}
var createGoTopPanel=function(cont)
{
    var gotoPanel = new Element('div',{'class':'gotoPanel'});
    var gotoTop = new Element('a',{'id':'gotoTop','class':'gotoTop'});
    bindGotoTop(gotoTop);
    gotoPanel.adopt(gotoTop);

    var feedback = new Element('a',{'class':'feedback'});
    feedback.addEvent('click', showWebcomplainDialog);

    gotoPanel.adopt(feedback);

    window.document.body.adopt(gotoPanel);
    var aArray = gotoPanel.getElements('a');
    aArray.addEvent('mouseenter', function() {
        this.setStyle('background-color',configData.highlightColor);
    });
    aArray.addEvent('mouseleave', function() {
        this.setStyle('background-color','');
    });
}


/*
		function loadSkinStyle(url)
		{
				var head = document.getElementsByTagName("head")[0];

				if (head)
				{
					var link = new Element('link',{'id':'jspxnet-skin','rel':'stylesheet','type':'text/css','href':url});
					var linkArray = head.getElements?head.getElements('link'): $$('link');
					if (linkArray)
					{
						for (var i=0; i<linkArray.length;i++)
						{
							if (linkArray[i].href.indexOf('jspxnet-ui.css')!=-1)
							{
								link.inject(linkArray[i],'after');
							}
						}
					} else
						head.appendChild(link);
				}
		}
*/

window.addEvent('domready', function () {
    var fcs = $$('div.formContainer');
    if (fcs) {
        fcs.each(function(form){
            var showFormIndex = form.get('indexing');
            if (typeof(showFormIndex)!= 'undefined'&&showFormIndex=='false') return;
            if (form.get('showTab')=='true') return;
            form.set('showTab','true');

            new JIndexingTab({contexts:'div.formContainer fieldset',sideBar:(new Element("a",{'class':'indexing'})),togPanel:'div.togPanel',eventAction:'click',current:0,mode:'tab',buttonCss:'none',buttonCurrentCss:'currentDoor'});

            var helpTips = form.getElements('div.helpTip');
            if (helpTips) helpTips.each(function(helpDiv){
                var helpSlide = new Fx.Slide(helpDiv).hide();
                form.getElements('div.pageConfig a.help').addEvent('click', function(event){
                    event.stop();
                    helpSlide.toggle();
                });
            });
        });

        //滚动到顶部 begin
        if (typeof(showGotoTop)!= 'undefined'&&showGotoTop)  createGoTopPanel();
        //滚动到顶部 end
    }
});
